package com.hsav.kcxb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.kcbsdk.MyTools.AIconType;
import com.kcbsdk.MyTools.AIconTypeList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by LiB on 2017/6/9.
 */

public class SingerGroupingAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<String> groupNames;
    private ArrayList<Object> data;
    private ArrayList<AIconTypeList> hotSingers;
    private ArrayList<AIconTypeList> allSingers;

    public SingerGroupingAdapter(Context context, ArrayList<AIconTypeList> hotSingers, ArrayList<AIconTypeList> allSingers)
    {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.hotSingers = hotSingers;
        this.allSingers = allSingers;
//        CreateSimpleData();
        CreateData();
    }

    private void CreateData() {
        if (hotSingers == null || allSingers == null) return;

        Map<String, List<AIconTypeList>> map = new HashMap<>();
        for (AIconTypeList item : allSingers) {
            List<AIconTypeList> list;
            String letter = String.valueOf(item.PinYin.charAt(0)).toUpperCase();
            if (map.containsKey(letter))
            {
                list = map.get(letter);
            }
            else
            {
                list = new ArrayList<>();
                map.put(String.valueOf(letter), list);
            }
            list.add(item);
        }
        List<String> letters = new ArrayList<>(map.keySet());
        Collections.sort(letters);

        groupNames = new ArrayList<>();
        groupNames.add("热门");
        groupNames.addAll(letters);

        data = new ArrayList<>();
        data.add(groupNames.get(0));
        data.addAll(hotSingers);
        for (int i = 1; i < groupNames.size(); i++)
        {
            data.add(groupNames.get(i));
            data.addAll(map.get(groupNames.get(i)));
        }
    }

    private void CreateSimpleData() {
        groupNames = new ArrayList<>();
        groupNames.addAll(Arrays.asList("热门", "A", "B"));
        List<String> hotItems = Arrays.asList("陈奕迅", "周杰伦", "刘德华", "张学友");
        List<String> aItems = Arrays.asList("阿宝", "安东尼");
        List<String> bItems = Arrays.asList("白鹤", "保剑锋", "比国有");
        data = new ArrayList<>();
        data.add(groupNames.get(0));
        data.addAll(hotItems);
        data.add(groupNames.get(1));
        data.addAll(aItems);
        data.add(groupNames.get(2));
        data.addAll(bItems);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        return !groupNames.contains(data.get(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (groupNames.contains(data.get(position)))
        {
            ViewHolder viewHolder;
            if (convertView == null || (viewHolder = (ViewHolder) convertView.getTag()).textViewGroupName == null)
            {
                convertView = inflater.inflate(R.layout.item_grouping, parent, false);

                viewHolder = new ViewHolder();
                viewHolder.textViewGroupName = (TextView) convertView.findViewById(R.id.text_view_group_name);
                convertView.setTag(viewHolder);
            }
            viewHolder.textViewGroupName.setText((String)data.get(position));
        }
        else
        {
            ViewHolder viewHolder;
            if (convertView == null || (viewHolder = (ViewHolder) convertView.getTag()).textViewSingerName == null)
            {
                convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);

                viewHolder = new ViewHolder();
                viewHolder.textViewSingerName = (TextView) convertView;
                convertView.setTag(viewHolder);
            }
            viewHolder.textViewSingerName.setText(((AIconTypeList)data.get(position)).TypeName);
        }
        return convertView;
    }

    class ViewHolder
    {
        TextView textViewGroupName;
        TextView textViewSingerName;
    }
}
